<?php
/**
 * Jamnews Ajax Handlers
 *
 * Handles AJAX requests via wp_ajax hook (both admin and front-end events)
 *
 * @author 		Jamnews
 * @category 	Core
 * @package 	Jamnews/Functions/AJAX
 * @version     1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly


/**
 * Admin ajax check post type is exists.
 *
 * Handles the insert new the post type in admin.
 *
 * @access public
 * @return void
 */
 
 function jamnews_ajax_check_posttype_exists(){
	global $jamnews;
	header('Content-Type: application/json');
	$result = true;
	$name = $_REQUEST['jamnews_post_type'];
	if($id = $_REQUEST['id']){
		$cpost_type = $jamnews->db_get_post_type($id);
	}
	$post_types = array_merge( 
		get_post_types(),
		$jamnews->db_get_post_types("all","keys") 
	);
	foreach($post_types as $name_posttype){
		// Check update post type
		if( $id && $name_posttype==$cpost_type->meta_key ){
			continue;
		}
		// Check inser new post type
		if($name_posttype==$name){
			$result = false;
			break;
		}
	}
	echo json_encode($result);
	die();
 }
 add_action("wp_ajax_post_type_is_exists","jamnews_ajax_check_posttype_exists");
?>